package com.finger.orderservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.finger.modelservice.enums.RefundStatusEnum;
import com.finger.modelservice.model.order.PaymentInfo;
import com.finger.modelservice.model.order.RefundInfo;
import com.finger.orderservice.mapper.RefundInfoMapper;
import com.finger.orderservice.service.RefundInfoService;
import org.springframework.stereotype.Service;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {

        // 根据订单id和支付类型从数据库查找退款记录
        RefundInfo refundInfo =
                baseMapper.selectOne(new QueryWrapper<RefundInfo>().eq("order_id", paymentInfo.getOrderId()).eq("payment_type", paymentInfo.getPaymentType()));

        // 如果退款记录不为空，则返回
        if(refundInfo!=null) return refundInfo;

        // 如果退款记录为空 则添加
        refundInfo = new RefundInfo();
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        baseMapper.insert(refundInfo);
        return refundInfo;
    }
}
